// pages/index/vote/vote_detail/vote_detail.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: {},
    id: null,
    state: false,
    isEnd: false,
    radio: 0,
    data: null,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "社区投票"
    })
    this.setData({
      theme: app.globalData.theme,
      id: options.id,
      state: options.state == '1' ? true : false,
      isEnd: options.isEnd == '0' ? true : false
    })
    Toast.loading({
      duration: 0,
      message: '加载中...',
    });
    this.getVoteDetail()
    this.addVisit()
  },

  /**
   * 获取投票详情
   */
  getVoteDetail: function () {
    http.get("questions/open/getAppletPaper",{
      paperid: this.data.id,
      state: this.data.state ? 1 : 0
    }).then(res => {
      if (res.data.code === http.REQUEST_OK){
        let data = res.data.data
        if (this.data.state) {
          data.array.forEach((main,m) => {
            //单选
            if (main.is_radio == '1') {
              main.list.forEach((sub,s) => {
                if (sub.id == main.radio[0].x_id) {
                  log.e("单选：" + JSON.stringify(sub))
                  main.temp = s + 1
                }
              })
            } else if (main.is_radio == '2') {
              //多选
              main.list.forEach(sub => {
                main.radio.forEach(r => {
                  if (sub.id == r.x_id) {
                    sub.is_check = true
                  }
                })
              })
            } else if (main.is_radio == '3') {
              //输入框
              main.text = main.radio[0].answer
            }
          })
        }
        this.setData({
          data: data
        })
        Toast.clear()
      }else {
        Toast.clear()
        Dialog.alert({
          title: "获取数据失败",
          message: res.data.msg
        }).then(() => {

        })
      }
    }, err => {
      Toast.fail("网络异常")
    })
  },

  /**
   * 添加浏览量
   */
  addVisit: function () {
    http.put("questions/open/addVisit",{
      paperid: this.data.id
    }).then()
  },

  /**
   * 单选框点击
   * @param e
   */
  onRadioChange: function (e) {
    if (this.data.isEnd == false) {
      const indexMain = e.currentTarget.dataset.index
      this.data.data.array[indexMain].temp = e.detail
      this.setData({
        data: this.data.data
      })
    }
  },

  /**
   * 多选框点击
   * @param e
   */
  onCheckSelect: function (e) {
    if (this.data.state == false && this.data.isEnd == false) {
      const main = e.currentTarget.dataset.main
      const sub = e.currentTarget.dataset.sub
      this.data.data.array[main].list[sub].is_check = !this.data.data.array[main].list[sub].is_check
      this.setData({
        data: this.data.data
      })
    }
  },

  /**
   * 获取输入框数据
   * @param e
   */
  onTextAreaInput: function (e) {
    const main = e.currentTarget.dataset.main
    this.data.data.array[main].text = e.detail.value
    log.e(this.data.data.array[main].text)
    this.setData({
      data: this.data.data
    })
  },

  /**
   * 提交数据
   * @param e
   */
  onSubmit: function (e) {
    const _this = this
    let list = []
    this.data.data.array.forEach(main => {
      //初始数据
      let data = {
        appletUserId: app.globalData.userInfo.applet_user_id,
        nickname: app.globalData.userInfo.nickname,
        paperid: this.data.id,
        questionsid: main.id,
      }
      if (main.is_radio == '1') {
        //单选框
        if (main.temp == null) {
         Dialog.alert({
           title: "提交失败",
           message: main.title + "\n还未选择,请选择后再试"
         }).then(() => {
           return null
         })
        }
        main.list.forEach((sub,index) => {
          if (main.temp - 1 == index) {
            data.optionid = sub.id
          }
        })
        list.push(data)
      } else if (main.is_radio == '2') {
        //多选框
        let isCheck = true
        main.list.forEach((sub,index) => {
          if (sub.is_check) {
            isCheck = false
            log.e("多选: " + sub.id)
            list.push({
              appletUserId: app.globalData.userInfo.applet_user_id,
              nickname: app.globalData.userInfo.nickname,
              paperid: _this.data.id,
              questionsid: main.id,
              optionid: sub.id
            })
          }
        })
        if (isCheck) {
          Dialog.alert({
            title: "提交失败",
            message: main.title + "\n至少需要选择一项,请选择后再试"
          }).then(() => {
            return null
          })
        }
      } else if (main.is_radio == '3') {
        //输入框
        if (main.text == null || main.text == ''){
          Dialog.alert({
            title: "提交失败",
            message: main.title + "\n请输入答案后再试"
          }).then(() => {
            return null
          })
        }
        data.answer = main.text
        list.push(data)
      }
    })
    log.e(JSON.stringify(list))
    http.putJson("questions/open/addQuesAnswer",list).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        Dialog.alert({
          title: "温馨提示",
          message: "提交成功"
        }).then(() => {
          wx.navigateBack()
        })
      }else {
        Dialog.alert({
          title: "提交失败",
          message: res.data.msg
        }).then(() => {

        })
      }
    }, err => {
      Toast.fail("网络异常")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})